#include "CanTester.h"
#include "canthread.h"
#include "ControlCAN.h"
#include <QTime>
#include <QCoreApplication>
#include <QMetaType>
#include <string.h>
#include <QDebug>

CanTester::CanTester(QObject *parent) : QObject(parent),
    m_deviceIndex(0),
    m_canBaundRate(500),
    m_canChannel(0),
    m_canFrameStandardOrExtend('0'),
    m_canFrameDataOrRemote('0'),
    m_canId0x("00000000")
{

    canthread = new CANThread();
    connect(canthread,&CANThread::boardInfo,this,&CanTester::onBoardInfo);
}

void CanTester::sendTestData() {
   // qDebug() << "发送测试数据到 CAN 口";
    // 在这里实现CAN发送逻辑
}

void CanTester::receiveData() {
    // qDebug() << "从 CAN 口接收数据";
    // 在这里实现CAN接收逻辑
}

void CanTester::canQmlToCpp(quint16 canBaundRate)
{
    m_canBaundRate = canBaundRate;
    qDebug() << "cpp revicer canBaundRate: " << m_canBaundRate;
}

void CanTester::canQmlToCppdeviceIndex(quint16 deviceIndex)
{
    m_deviceIndex = deviceIndex;
    qDebug() << "cpp revicer deviceIndex: " << m_deviceIndex;
}


bool CanTester::canOpenDevice()
{
    bool dev = canthread->openDevice(4, m_deviceIndex, m_canBaundRate);
    return dev;
}

bool CanTester::canInitDevice()
{
    bool dev = canthread->initCAN();
    return dev;
}


bool CanTester::canStartDevice()
{
    if(canthread->startCAN())
    {
        canthread->start();
        return true;
    }
    else
        qDebug() << "startCAN failed !!!";    // 20241205 zd 这里需要增加can未启动的消息提醒机制
    return false;
}

void CanTester::canCloseDevice()
{
    canthread->stop();
    canthread->closeDevice();
}

bool CanTester::canResetDevice()
{
    bool dev = canthread->reSetCAN();
    return dev;
}

QString CanTester::versionStr(USHORT ver)
{
    return "V" + QString::number((ver & 0x0FFF) /0x100,16).toUpper() + "." + QString("%1 ").arg((ver & 0x0FFF) % 0x100,2,16,QChar('0')).toUpper();
}

void CanTester::onBoardInfo(VCI_BOARD_INFO vbi)
{
    _rcvCan_Num = QString::number(vbi.can_Num);
    _rcvHw_Version = versionStr(vbi.hw_Version);
    _rcvDr_Version = versionStr(vbi.in_Version);
    _rcvIr_Version = QString::number(vbi.irq_Num);
}

void CanTester::canChannel(int channel)
{
    m_canChannel = channel;
    qDebug() << "set canChannel: " << channel;
}

void CanTester::canFrameStandardOrExtend(BYTE type)
{
    m_canFrameStandardOrExtend = type; // 0 标准 1 扩展
    qDebug() << "set canFrameStandardOrExtend: " << type;
}

void CanTester::canFrameDataOrRemote(BYTE type)
{
    m_canFrameDataOrRemote = type;
    qDebug() << "set canFrameDataOrRemote: " << type;
}

void CanTester::canId0x(QString canId0x)
{
    m_canId0x = canId0x;
    qDebug() << "set canId0x: " << canId0x;
}

void CanTester::canSendData(QString canSenddata)
{
    m_canSendData = canSenddata;
    QStringList strList = m_canSendData.split(" ");
    unsigned char data[8];
    memset(data,0,8);
    UINT dlc = 0;
    dlc = strList.count() > 8 ? 8 : strList.count();
    int sendId = QVariant(m_canId0x.toInt(Q_NULLPTR,16)).toUInt();
    for(int i = 0;i < dlc;i ++)
        data[i] = strList.at(i).toInt(Q_NULLPTR,16);
    if(canthread->sendData(m_canChannel,
                            QVariant(m_canId0x.toInt(Q_NULLPTR,16)).toUInt(),
                            m_canFrameDataOrRemote,
                            m_canFrameStandardOrExtend,
                            data,dlc))
    {
        QStringList messageList;

        messageList.clear();
        messageList << QTime::currentTime().toString("hh:mm:ss zzz");//时间
        messageList << "w";//时间
        messageList << "w";//时间
        messageList << "CH" + QString::number(m_canChannel);
        messageList << "w";//收发
        messageList << "0x" + m_canId0x;//ID
        messageList << ((m_canFrameDataOrRemote == 0) ? "w" : "w");//类型
        messageList << ((m_canFrameStandardOrExtend == 0) ? "w" : "w");//Frame
        QString str = "";
        if(m_canFrameDataOrRemote == 0)//数据帧显示数据
        {
            messageList << "0x" + QString::number(dlc,16).toUpper();//长度
            str = "x| ";
            for(int j = 0;j < dlc;j ++)
                str += QString("%1 ").arg((unsigned char)data[j],2,16,QChar('0')).toUpper();//QString::number((unsigned char)data[j],16) + " ";
        }
        else
            messageList << "0x0";//长度
        messageList << str;//数据
        qDebug() << messageList;
        //AddDataToList(messageList);
    }
}



